/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package IU;

import Logica.Carrera;
import Logica.Fachada;
import Logica.Hipodromo;
import Logica.Jornada;
import Utilidades.ManejoFechas;
import Utilidades.Observable;
import Utilidades.Observador;
import java.util.ArrayList;
import java.util.Date;

/**
 *
 * @author jarzarello
 */
public class CarreraMonitorear extends javax.swing.JDialog implements Observador {

    private final Hipodromo hs = PanelPrincipal.getInstancia().getHipodromoSeleccionado();
    private final ManejoFechas mf = new ManejoFechas();
    private Carrera carreraActual;
    private Jornada jornadaHoy;
    
    public Hipodromo getHipodromo() {
        return hs;
    }

    public ManejoFechas getManejoFechas() {
        return mf;
    }

    public Carrera getCarreraActual() {
        return carreraActual;
    }

    public void setCarreraActual(Carrera carreraActual) {
        this.carreraActual = carreraActual;
    }

    public Jornada getJornadaHoy() {
        return jornadaHoy;
    }

    public void setJornadaHoy(Jornada jornadaHoy) {
        this.jornadaHoy = jornadaHoy;
    }
    /**
     * Creates new form CarreraMonitorear
     */
    public CarreraMonitorear(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        Fachada.getInstancia().agregar(this);
        cargarCarreras();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        lblFecha = new javax.swing.JLabel();
        jSeparator1 = new javax.swing.JSeparator();
        lblListaCarreras = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        lstCarreras = new javax.swing.JList();
        btnCancelar = new javax.swing.JButton();
        btnSeleccionar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);

        lblFecha.setText("Fecha:");

        lblListaCarreras.setText("Lista de Carreras:");

        lstCarreras.setSelectionMode(javax.swing.ListSelectionModel.SINGLE_SELECTION);
        jScrollPane1.setViewportView(lstCarreras);

        btnCancelar.setText("Cancelar");
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        btnSeleccionar.setText("Seleccionar");
        btnSeleccionar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnSeleccionarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jSeparator1)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(lblFecha)
                            .addComponent(lblListaCarreras))
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 380, Short.MAX_VALUE)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(btnSeleccionar)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(btnCancelar)))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lblFecha)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 10, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(lblListaCarreras)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnCancelar)
                    .addComponent(btnSeleccionar))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        setBounds(0, 0, 416, 275);
    }// </editor-fold>//GEN-END:initComponents

    private void btnSeleccionarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnSeleccionarActionPerformed
        // TODO add your handling code here:
        cargarCarrera();
    }//GEN-LAST:event_btnSeleccionarActionPerformed

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        dispose();
    }//GEN-LAST:event_btnCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnSeleccionar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    private javax.swing.JLabel lblFecha;
    private javax.swing.JLabel lblListaCarreras;
    private javax.swing.JList lstCarreras;
    // End of variables declaration//GEN-END:variables

    private void cargarCarreras() {
        
        Date fechaHoy = mf.formatoFecha(new Date());
        jornadaHoy = hs.buscarJornada(fechaHoy);
        ArrayList<String> carrerasAMostrar = new ArrayList();
        
        for(Carrera c:jornadaHoy.getCarreras()){
            carrerasAMostrar.add(formatearCarrera(c));
        }
        lstCarreras.setListData(carrerasAMostrar.toArray());
    }
    
    private String formatearCarrera (Carrera c) {
        return "N°: " + c.getNumero() + " - " + c.getNombre()+ " - " + c.getEstadoString();
    }

    @Override
    public void actualizar(Observable origen, Object param) {
        if(param.equals(Fachada.Evento.CA_DEFINIDA) || param.equals(Fachada.Evento.CA_ABIERTA) 
                || param.equals(Fachada.Evento.CA_CERRADA) || param.equals(Fachada.Evento.CA_FINALIZADA))
            cargarCarreras();
    }

    private void cargarCarrera() {

        Carrera c = (Carrera) lstCarreras.getSelectedValue();
        
        CarreraMonitor cm = new CarreraMonitor(null, true);
        cm.setCarrera(c);
        cm.setVisible(true);
    }
}
